package com.study.d015propertybind.fx;


import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.util.Callback;

/**
*@description FXCollections工具类中
*observableArrayList(Callback<E,Observable[]> extractor) 方法的使用
*observableList(List<E> list, Callback<E,Observable[]> extractor) 方法的使用
*@author xtayhame
*date 2020年3月6日
*version
*since v1.0
*/
public class CallBack {
	public static void main(String[] args) {
		System.out.println("-----------------------------------observableArrayList(Callback<E,Observable[]> extractor) 方法的使用------------------------------------------------");

		SimpleStringProperty s1 = new SimpleStringProperty("A");
		SimpleStringProperty s2 = new SimpleStringProperty("B");


		ObservableList<SimpleStringProperty> list= FXCollections.observableArrayList(new Callback<SimpleStringProperty, Observable[]>() {
			@Override
			public Observable[] call(SimpleStringProperty param) {
				//只有对list进行add,remove等操作时才会执行
				System.out.println("每次传过来的参数->param="+param);
				Observable[] obs = new Observable[]{param};
				return obs;
			}
		});

		list.addListener(new ListChangeListener<SimpleStringProperty>() {
			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends SimpleStringProperty> c) {
				if(c.next()){
					System.out.println("change:"+c +  "    wasUpdated:" + c.wasUpdated());
				}
			}
		});

		list.add(s1);
		//调用s1.set() 方法时，c.wasUpdated()为true; 因为这里监听中的参数类型SimpleStringProperty，所以只有修改具体的值的时候，wasUpdated才是true
		s1.set("C");
		list.add(s2);
		System.out.println("-----------");
		list.remove(s2);
		//list.forEach(item->System.out.print(item));


		System.out.println("------------------------observableList(List<E> list, Callback<E,Observable[]> extractor) 方法的使用--------------------------------------------------------------");
		SimpleStringProperty sp1= new SimpleStringProperty("E");
		SimpleStringProperty sp2= new SimpleStringProperty("F");

		ObservableList<SimpleStringProperty> list2=FXCollections.observableArrayList(new Callback<SimpleStringProperty, Observable[]>() {
			@Override
			public Observable[] call(SimpleStringProperty param) {
				System.out.println("call-02:"+param);
				Observable[] ob1 = new Observable[]{param};
				return ob1;
			}
		});

		ObservableList<SimpleStringProperty> list3=FXCollections.observableList(list2, new Callback<SimpleStringProperty, Observable[]>() {
			@Override
			public Observable[] call(SimpleStringProperty param) {
				System.out.println("call-03:"+param);
				Observable[] ob2 = new Observable[]{param};
				return ob2;
			}
		});


		list2.addListener(new ListChangeListener<SimpleStringProperty>() {
			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends SimpleStringProperty> c) {
				if(c.next()){
					System.out.println("change02:"+c + "       wasUpdated:"+c.wasUpdated());
				}
			}
		});

		list3.addListener(new ListChangeListener<SimpleStringProperty>() {
			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends SimpleStringProperty> c) {
				if(c.next()){
					System.out.println("change03:"+c + "       wasUpdated:"+c.wasUpdated());
				}
			}
		});

		//类似双向绑定，会同时向list2, list3中添加元素，change和call会分别被调用两次    call-03-->call-02-->chang02-->change03
		list3.add(sp1);

		System.out.println("list2="+list2 + "     list3="+list3);
		System.out.println("-----------------------------------");
		sp1.set("M");  // 会同事调用change02和change03， list2和list3的值是一样的
		System.out.println("list2="+list2 + "     list3="+list3);
		System.out.println("------只对list2进行操作------");
		//只会调用call-02 和 change02 ,list2和list3的值是一样的
		list2.add(new SimpleStringProperty("P"));
		System.out.println("list2="+list2 + "     list3="+list3);

	}
}
